﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using jiaju.Areas.Back.Models.DAL;
using jiaju.Areas.Back.Models.DBModel;
using jiaju.Areas.Back.Models;
namespace jiaju.Areas.Back.Controllers
{
    [Authorize]
    public class MessageController : Controller
    {
        // GET: Back/Message
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetMessageList(int page, int limit, string name)
        {
            //调用servers方法
            List<Message> datalist = MessageService.getMessage(page, limit, name);
            //获取count 总条数
            string sql = "select COUNT(*) from dbo.Message";
            int count = (int)DBHelp.ExcuteScalar(sql);
            Object obj = new
            {
                code = 0,
                msg = "请求成功",
                count = count,
                data = datalist

            };
            return Json(obj, JsonRequestBehavior.AllowGet);
        }
        //新增留言
        public ActionResult Add()
        {
            return View();
        }
        //新增数据
        //参数名可以用类接受
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddDel(Message message)
        {
            //新增，请求数据库
            int n = MessageService.addMessage(message);
            if (n > 0)
            {
                return Json(new { success = true, msg = "新增成功" });
            }
            else
            {
                return Json(new { success = false, msg = "新增失败" });
            }
        }
        //删除商品分类方法DelArt
        public ActionResult DelArt(String Id)
        {
            int n = MessageService.DelMessage(Id);
            if (n > 0)
            {
                return Json(new { success = true, msg = "删除成功" });
            }
            else
            {
                return Json(new { success = false, msg = "删除失败" });
            }
        }
        //编辑留言 Edit
        public ActionResult Edit(string Id)
        {
            //通过Id获取数据详情
            Message mes =MessageService.GetById(Id);
            //把详情数据传到页面
            ViewBag.A = mes;
            return View();
        }
        public ActionResult Status(string Id)
        {
            int n = MessageService.UpdateStatus(Id, "已读");
            if ( n > 0)
            {
                return Json(new { success = true, msg = "编辑成功" });
            }
            else
            {
                return Json(new { success = false, msg = "编辑失败" });
            }
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditDel(Message mes)
        {
            //新增，请求数据库
            int m = MessageService.EditMessage(mes);
            if (m > 0)
            {
                return Json(new { success = true, msg = "编辑成功" });
            }
            else
            {
                return Json(new { success = false, msg = "编辑失败" });
            }
        }
        public ActionResult Answer(string Id)
        {
            ViewBag.A = Id;
            return View();
        }
        public ActionResult AnswerDel(string Id,string Answer)
        {
            int n = MessageService.UpdateAnswer(Id,Answer);
            int m = MessageService.UpdateStatus(Id,"已回复");
            if (n > 0)
            {
                return Json(new { success = true, msg = "回复成功" });
            }
            else
            {
                return Json(new { success = false, msg = "回复失败" });
            }
        }
    }
}